package com.qiku.controller;

import com.qiku.domin.Course;
import com.qiku.domin.CourseLesson;
import com.qiku.domin.CourseSection;
import com.qiku.domin.ResponseResult;
import com.qiku.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/findSectionAndLesson" , method = RequestMethod.GET)
    public ResponseResult findSectionAndLesson(@RequestParam Integer courseId){
        List<CourseSection> id = courseService.findSectionAndLessonByCourseId(courseId);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", id);
        return result;
    }

    @RequestMapping(value = "/findCourseByCourseId" , method = RequestMethod.GET)
    public ResponseResult findCourseByCourseId(@RequestParam int courseId){
        Course courseByCourseId = courseService.findCourseByCourseId(courseId);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", courseByCourseId);
        return result;
    }



    @RequestMapping(value = "saveOrUpdateSection" , method = RequestMethod.POST)
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection){
        ResponseResult result = null;
        if (null != courseSection.getId() && 0 < courseSection.getId()){
            // 有id 修改操作
            courseService.updateCourseSection(courseSection);
            result = new ResponseResult(true, 200, "修改成功", null);
            return result;

        }else {
            courseService.saveCourseSection(courseSection);
            result = new ResponseResult(true, 200, "保存成功", null);
            return result;
        }
    }


    @RequestMapping(value = "updateSectionStatus" , method = RequestMethod.GET)
    public ResponseResult updateSectionStatus(Integer id ,@RequestParam int status){
        courseService.updateStatusCourseSection(id , status);
        Map<String , Integer> map = new HashMap<>();
        Integer stutas = map.put("stutas", status);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", stutas);
        return result;
    }

    //    @PostMapping
    @RequestMapping(value = "/saveOrUpdateLesson" , method = RequestMethod.POST)
    public ResponseResult saveLesson(@RequestBody CourseLesson courseLesson){
        courseService.saveLesson(courseLesson);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", null);
        return result;
    }

}
